import '../entities/participant_entity.dart';

/// 🎯 参与者审核响应模型
/// 
/// 封装后端返回的审核操作结果
/// 对应接口: POST /activities/participants/review/
class ParticipantReviewResp {
  /// 响应状态
  final String status;
  
  /// 响应消息
  final String message;
  
  /// 活动ID
  final int? activityId;
  
  /// 用户ID
  final int? userId;
  
  /// 审核结果状态
  final String? reviewStatus;
  
  /// 审核时间
  final String? reviewTime;
  
  /// 额外数据
  final Map<String, dynamic>? extraData;

  ParticipantReviewResp({
    required this.status,
    required this.message,
    this.activityId,
    this.userId,
    this.reviewStatus,
    this.reviewTime,
    this.extraData,
  });

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 是否审核通过
  bool get isApproved => reviewStatus == 'approved';

  /// 是否审核拒绝
  bool get isRejected => reviewStatus == 'rejected';

  /// 从JSON创建实例
  factory ParticipantReviewResp.fromJson(Map<String, dynamic> json) {
    return ParticipantReviewResp(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '审核操作失败',
      activityId: json['activity_id'] is String 
          ? int.tryParse(json['activity_id'])
          : json['activity_id'],
      userId: json['user_id'] is String 
          ? int.tryParse(json['user_id'])
          : json['user_id'],
      reviewStatus: json['review_status'],
      reviewTime: json['review_time'],
      extraData: json['extra_data'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      if (activityId != null) 'activity_id': activityId,
      if (userId != null) 'user_id': userId,
      if (reviewStatus != null) 'review_status': reviewStatus,
      if (reviewTime != null) 'review_time': reviewTime,
      if (extraData != null) 'extra_data': extraData,
    };
  }

  @override
  String toString() {
    return 'ParticipantReviewResp{status: $status, message: $message, activityId: $activityId, userId: $userId, reviewStatus: $reviewStatus}';
  }
}

/// 🎯 发布者活动参与者列表响应模型
/// 
/// 封装后端返回的参与者列表数据
/// 对应接口: /publisher/activity/participants/
class PublisherParticipantsResp {
  /// 响应状态
  final String status;
  
  /// 响应消息
  final String message;
  
  /// 参与者数据列表
  final List<ParticipantEntity> participants;
  
  /// 总数
  final int total;
  
  /// 发布者ID
  final int? publisherId;

  PublisherParticipantsResp({
    required this.status,
    required this.message,
    required this.participants,
    required this.total,
    this.publisherId,
  });

  /// 是否成功
  bool get isSuccess => status == 'success';
  
  /// 待审核的参与者
  List<ParticipantEntity> get pendingParticipants => 
      participants.where((p) => p.isPending).toList();
  
  /// 已通过的参与者
  List<ParticipantEntity> get approvedParticipants => 
      participants.where((p) => p.isApproved).toList();
  
  /// 已拒绝的参与者
  List<ParticipantEntity> get rejectedParticipants => 
      participants.where((p) => p.isRejected).toList();

  /// 从JSON创建实例
  factory PublisherParticipantsResp.fromJson(Map<String, dynamic> json) {
    // 服务器返回的数据结构：{status, data: {total, activities, participants, pagination}}
    final dataMap = json['data'] as Map<String, dynamic>? ?? {};
    final participantsList = dataMap['participants'] as List? ?? [];
    final totalCount = dataMap['total'] as int? ?? 0;
    
    return PublisherParticipantsResp(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '查询失败',
      participants: participantsList
          .map((item) => ParticipantEntity.fromJson(item as Map<String, dynamic>))
          .toList(),
      total: totalCount,
      publisherId: json['publisher_id'] is String 
          ? int.tryParse(json['publisher_id'])
          : json['publisher_id'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'data': participants.map((p) => p.toJson()).toList(),
      'total': total,
      if (publisherId != null) 'publisher_id': publisherId,
    };
  }

  @override
  String toString() {
    return 'PublisherParticipantsResp{status: $status, total: $total, participants: ${participants.length}}';
  }
}

